﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Xml;

namespace Client_Server_Interface
{
    public class XmlWriterHelper : IDisposable
    {
        private StringWriter _stream;
        private XmlWriter _xw;

        private XmlWriterHelper()
        {
            
        }

        private void Init()
        {
            _stream = new StringWriter();
            _xw = XmlWriter.Create(_stream);
        }

        public static XmlWriterHelper CreateInstance()
        {
            var h = new XmlWriterHelper();
            h.Init();
            return h;
        }

        public static XmlWriterHelper CreateInstance(string root)
        {
            //var h = new XmlWriterHelper();
            //h.Init();

            var h = CreateInstance();
            h._xw.WriteStartElement(root);
            var s = h._stream.ToString();
            return h;
        }

        public void AddElement(string elName)
        {
            _xw.WriteStartElement(elName);
        }

        public void AddAttribute(string atrName, string atrValue)
        {
            //_xw.WriteStartAttribute(atrName);
            _xw.WriteAttributeString(atrName, atrValue);
        }

        public string EndElement()
        {
            _xw.WriteEndElement();
            _xw.Flush();
            return _stream.ToString();
        }

        public new string ToString()
        {
            return _xw.ToString();
        }

        public void Dispose()
        {
            _xw.Close();
            _stream.Close();
        }
    }
}
